
import { ActorParams as Params } from '@swda/configs/params'

let SWDA_ActorSprites =  {};

// 动作参数
Sprite_Actor.MOTIONS = { ...Params.actions };
SWDA_ActorSprites.actorMotionParams = {...Params.actorMotionParams};
SWDA_ActorSprites.actorMotionMapping = {...Params.actorMotionMapping};
SWDA_ActorSprites.actorMotionSpeed = {...Params.actorMotionSpeed};
//=============================================================================
// Sprite_Actor
//=============================================================================

SWDA_ActorSprites.Sprite_Actor_InitMembers = Sprite_Actor.prototype.initMembers;
Sprite_Actor.prototype.initMembers = function() {
    this._col = 0;
    this._row = 0;
    this._motionMapping = SWDA_ActorSprites.actorMotionMapping['FZC'];
    this._motionSpeed = SWDA_ActorSprites.actorMotionSpeed['FZC'];
    SWDA_ActorSprites.Sprite_Actor_InitMembers.call(this);
};

SWDA_ActorSprites.Sprite_Actor_SetBattler = Sprite_Actor.prototype.setBattler;
Sprite_Actor.prototype.setBattler = function(battler) {
    SWDA_ActorSprites.Sprite_Actor_SetBattler.call(this,battler);
    this.refreshParam();
};

Sprite_Actor.prototype.refreshParam = function() {
    if (this._battlerName){
        this._row = SWDA_ActorSprites.actorMotionParams[this._battlerName].row;
        this._col = SWDA_ActorSprites.actorMotionParams[this._battlerName].col;
        this._motionMapping = SWDA_ActorSprites.actorMotionMapping[this._battlerName];
        this._motionSpeed = SWDA_ActorSprites.actorMotionSpeed[this._battlerName];
    }
}

// 刷新帧绘制
Sprite_Actor.prototype.updateFrame = function() {
    Sprite_Battler.prototype.updateFrame.call(this);
    var bitmap = this._mainSprite.bitmap;
    if (bitmap) {
        
        var motionIndex = this._motion ? this._motion.index : 0;
       
        var frames = this._motionMapping[motionIndex];
        
        var pattern = this._pattern < frames.length ? this._pattern : 0;
        var cw = bitmap.width / this._col;
        var ch = bitmap.height / this._row;
        var cx = ((frames[pattern] % this._col - 1) < 0 ) ? (this._col - 1) : (frames[pattern] % this._col - 1 );
        var cy = Math.ceil(frames[pattern] / this._col) - 1;
        this._mainSprite.setFrame(cx * cw , cy * ch + 1, cw, ch - 1);
    }
	
	//YEP_BattleEngineCore
	if (Imported.YEP_BattleEngineCore){
		if (!this._mainSprite) return;
		if (!this._mainSprite.bitmap) return;
		if (this._mainSprite.bitmap.width > 0 && !this.bitmap) {
		  var sw = this._mainSprite.bitmap.width / this._col;
		  var sh = this._mainSprite.bitmap.height / this._row;
		  this.bitmap = new Bitmap(sw, sh);
		}
	}
};

// 刷新动作
Sprite_Actor.prototype.updateMotion = function() {
  this.updateDorcedMotion();
  this.updateMotionCount();
};

// 如果强制显示角色战斗图，应强制显示待机动作
Sprite_Actor.prototype.updateDorcedMotion = function() {
  //排除三晋天师情况
  if (this._battlerName =='FZCfb' || this._battlerName =='FZCfr' || this._battlerName =='FZCfw')
    return;
  const actor = this._actor;
  const actorId = actor.actorId();
  if(BattleManager.showActorSprite(actorId) && this._motion.index != 0)
    this.startMotion(actor.idleMotion());
};


// 刷新动画帧数
Sprite_Actor.prototype.updateMotionCount = function() {
    var motionIndex = this._motion ? this._motion.index : 0;
    var frameNum = this._motionMapping[motionIndex].length;
    if (this._motion) {
        if( ++this._motionCount >= this._motionSpeed[this._motion.index]) {
            if (this._motion.loop) {
                this._pattern = (this._pattern + 1) % (frameNum + 1);
            } else if (this._pattern < (frameNum - 1)) {
                this._pattern++;
            } else {
                this.refreshMotion();
            }
            this._motionCount = 0;
        }
    }
};

// 修改动作动画刷新逻辑：等待技能执行时不变回待机动作。
Sprite_Actor.prototype.refreshMotion = function() {
    var actor = this._actor;
    if (!actor) return;
    var motionGuard = Sprite_Actor.MOTIONS['guard'];

    // 修复非防御状态不恢复动作的问题
    if ( (actor.isGuard() || actor.isGuardWaiting()) && (this._motion === motionGuard) && !BattleManager.isInputting()) return;
    //if (this._motion === motionGuard && !BattleManager.isInputting()) return;
    var stateMotion = actor.stateMotionIndex();
//    if (actor.isInputting() || actor.isActing()) {
    if (actor.isInputting()) {
      this.startMotion(actor.idleMotion());
    } else if (stateMotion === 3) {
      this.startMotion(actor.deadMotion());
    } else if (stateMotion === 2) {
      this.startMotion(actor.sleepMotion());
    } else if (actor.isChanting()) {
      this.startMotion(actor.chantMotion());
    } else if (actor.isGuard() || actor.isGuardWaiting()) {
      this.startMotion(actor.guardMotion());
    } else if (stateMotion === 1) {
      this.startMotion(actor.abnormalMotion());
    } else if (actor.isDying()) {
      this.startMotion(actor.dyingMotion());
    } else if (actor.isUndecided()) {
      this.startMotion(actor.idleMotion());
    } else if(this.isMoving()){
      this.startMotion(actor.waitMotion()); // 移动时（公共事件时）变为待机（行走）
    } else{
      //this.startMotion(actor.waitMotion());   //其他情况不变更
    }
};



